package com.course.component.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import sun.misc.BASE64Decoder;

import java.io.IOException;

/**
 * @Copyright: Copyright (c)  林中小鸟  All rights reserved.
 * @author: moon
 * @date: 2022/7/7 12:07
 * @Description: **********************
 */
public class EncryptUtil {
    //static byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded();
    //static String secretKey = new BASE64Encoder().encodeBuffer(key);
    static String secretKey = "KFJ-QIF-LAD-FJQ-IDL-FAL-LDF-WQQ";

    //info:需要加密的明文
    public static String encrypt(String info) {
        byte[] key = new byte[0];
        try {
            key = new BASE64Decoder().decodeBuffer(secretKey);
        } catch (IOException e) {
            e.printStackTrace();
        }
        DES des = SecureUtil.des(key);
        String encrypt = des.encryptHex(info);
        return encrypt;
    }

    public static String decrypt(String encrypt) {
        byte[] key = new byte[0];
        try {
            key = new BASE64Decoder().decodeBuffer(secretKey);
        } catch (IOException e) {
            e.printStackTrace();
        }
        DES des = SecureUtil.des(key);
        return des.decryptStr(encrypt);
    }

    public static byte[] encryptByt(String info) {
        byte[] key = new byte[0];
        try {
            key = new BASE64Decoder().decodeBuffer(secretKey);
        } catch (IOException e) {
            e.printStackTrace();
        }
        DES des = SecureUtil.des(key);
        byte[] encrypt = des.encrypt(info);
        return encrypt;
    }

    public static byte[] decryptByt(String encrypt) {
        byte[] key = new byte[0];
        try {
            key = new BASE64Decoder().decodeBuffer(secretKey);
        } catch (IOException e) {
            e.printStackTrace();
        }
        DES des = SecureUtil.des(key);
        return des.decrypt(encrypt);
    }
}
